#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: send_mail.sh
# Author: GID5564
# Description:
# Created Time: 01/12/23-21:42:11
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash

#发送HTML邮件
function send_html_email(){
    local to
    local enc_dir
    local file_path
    local enc=''
    local title=$1
    Msg  "${BLUE}请输入收件人地址: ${RESET}"
    read to	
    if [[ -z ${to} ]];then
        to="46djr39f@hpeprint.com"
    fi
	if [[ ${to} =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]];then
	    wait
	else
	    error "邮箱地址格式错误${YELLOW}${to}${RESET}"
	    return 1
	fi

    if [[ -z ${title} ]];then
        title="HTML邮件"
    fi
    
    Msg  "${BLUE}请输入附件所在完整目录: ${RESET}"
	read enc_dir
    if [[ -d ${enc_dir} ]];then
	   
	    for file_path in $(find ${enc_dir} -maxdepth 1 -type f)
	    do
	        enc="${enc}${file_path},"
	    done
	
	else
	    error "目录不存在!"
        return 1
	fi
	enc=${enc%?}

    local html="index.html"
    vim ${html}
    if [[ ! -f ${html} ]];then
        return 1
    fi
    send_html ${to} ${title} ${html} ${enc}
}

#微信远程打印
function wechat_remote_print(){
    local enc_dir
    local file_path
    local to
    warn "请确保已连接远程打印机!"
    Msg  "${BLUE}请输入收件人地址: ${RESET}"
    read to
    if [[ -z ${to} ]];then
        to="46djr39f@hpeprint.com"
    fi
	if [[ ${to} =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]];then
	    wait
	else
	    error "邮箱地址格式错误${YELLOW}${to}${RESET}"
	    return 1
	fi
	
	#打印资源的目录
	Msg  "${BLUE}请输入打印资源所在完整目录: ${RESET}"
	read enc_dir
	if [[ -d ${enc_dir} ]];then
	   
	    for file_path in $(find ${enc_dir} -maxdepth 1 -type f)
	    do
            send_print ${to} ${file_path}
            success "文件${file_path##*/}发送成功!"
	    done
	
	else
	    error "目录不存在!"
        return 1
	fi
}

